package cn.iocoder.yudao.module.blog.controller.admin.follow;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.follow.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.follow.BlogFollowDO;
import cn.iocoder.yudao.module.blog.convert.follow.BlogFollowConvert;
import cn.iocoder.yudao.module.blog.service.follow.BlogFollowService;

@Tag(name = "管理后台 - 关注")
@RestController
@RequestMapping("/blog/follow")
@Validated
public class BlogFollowController {

    @Resource
    private BlogFollowService followService;

    @PostMapping("/create")
    @Operation(summary = "创建关注")
    @PreAuthorize("@ss.hasPermission('blog:follow:create')")
    public CommonResult<Long> createFollow(@Valid @RequestBody BlogFollowCreateReqVO createReqVO) {
        return success(followService.createFollow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新关注")
    @PreAuthorize("@ss.hasPermission('blog:follow:update')")
    public CommonResult<Boolean> updateFollow(@Valid @RequestBody BlogFollowUpdateReqVO updateReqVO) {
        followService.updateFollow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除关注")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:follow:delete')")
    public CommonResult<Boolean> deleteFollow(@RequestParam("id") Long id) {
        followService.deleteFollow(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得关注")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:follow:query')")
    public CommonResult<BlogFollowRespVO> getFollow(@RequestParam("id") Long id) {
        BlogFollowDO follow = followService.getFollow(id);
        return success(BlogFollowConvert.INSTANCE.convert(follow));
    }

    @GetMapping("/list")
    @Operation(summary = "获得关注列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:follow:query')")
    public CommonResult<List<BlogFollowRespVO>> getFollowList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogFollowDO> list = followService.getFollowList(ids);
        return success(BlogFollowConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得关注分页")
    @PreAuthorize("@ss.hasPermission('blog:follow:query')")
    public CommonResult<PageResult<BlogFollowRespVO>> getFollowPage(@Valid BlogFollowPageReqVO pageVO) {
        PageResult<BlogFollowDO> pageResult = followService.getFollowPage(pageVO);
        return success(BlogFollowConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出关注 Excel")
    @PreAuthorize("@ss.hasPermission('blog:follow:export')")
    @OperateLog(type = EXPORT)
    public void exportFollowExcel(@Valid BlogFollowExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogFollowDO> list = followService.getFollowList(exportReqVO);
        // 导出 Excel
        List<BlogFollowExcelVO> datas = BlogFollowConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "关注.xls", "数据", BlogFollowExcelVO.class, datas);
    }

}
